﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Net
{
    public class NetUtil
    {
        /// <summary>
        /// 获取ipv4的ip地址
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<string> GetIPV4()
        {
            string hostName = Dns.GetHostName();
            IPAddress[] addressArr = Dns.GetHostAddresses(hostName);
            for (int index = 0; index < addressArr.Length; index++)
            {
                //从IP地址列表中筛选出IPv4类型的IP地址
                //AddressFamily.InterNetwork表示此IP为IPv4,
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                if (addressArr[index].AddressFamily == AddressFamily.InterNetwork)
                {
                    yield return addressArr[index].ToString();
                }
            }
        }
    }
}
